<?php
class shop_productControl extends skymvc{
	 
	public function __construct(){
		parent::__construct(); 
	}
	
	function onDefault(){
		 
		 
		$limit=48;
		$where=" status<99 AND shopid=".SHOPID;
		$url=APPSHOP."?m=shop_product&a=my";
		$shop_cid=get('shop_cid','i');
		if($shop_cid){
			 
			$where.=" AND shop_cid =".$shop_cid;
			$url.="&shop_cid=".$shop_cid;
		}
		
		$is_recommend=get('is_recommend','i');
		if($is_recommend){
			$where.=" AND is_recommend=".$is_recommend;
			$url.="&is_recommend=".$is_recommend;
		}
		
		$title=get('title','h');
		if($title){
			$where.=" AND title like '%".$title."%'";
			$url.="&title=".urlencode($title);
		}
		$start=get('per_page','i');
		$option=array(
			"start"=>$start,
			"limit"=>$limit,
			"order"=>"id DESC",
			"where"=>$where,
		);
		$rscount=true;
		$data=M("shop_product")->select($option,$rscount);
		 
		if($data){
			foreach($data as $k=>$v){
				$data[$k]['cname']=M("category")->selectOne(array("where"=>array("catid"=>$v['catid']),"fields"=>"cname"));
				if($v['catid_2nd']){
					$data[$k]['cname_2nd']=M("category")->selectOne(array("where"=>array("catid"=>$v['catid_2nd']),"fields"=>"cname"));
				}
				
				if($v['catid_3nd']){
					$data[$k]['cname_3nd']=M("category")->selectOne(array("where"=>array("catid"=>$v['catid_3nd']),"fields"=>"cname"));
				}
				
				
			}
		}
		$url=APPSHOP."?m=shop_product";
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$this->smarty->assign(
			array(
				"data"=>$data,
				"rscount"=>$rscount,
				"pagelist"=>$pagelist,
				"shop_cat_list"=>M("shop_category")->id_title(array("where"=>"shopid=".SHOPID)),
				"pid_list"=>M("shop_category")->children(array("where"=>" tablename='shop_product' AND pid=0 AND shopid=".SHOPID))
			)
		);
		$this->smarty->display("shop_product/index.html");
	}
	
	/*发表产品模型*/
	public function onAdd(){
		$cat_list=M("category")->children(0,MODEL_SHOP_PRODUCT_ID);;
		$id=get('id','i');
		if($id){
			$data=M("shop_product")->selectRow(array("where"=>"id=$id"));
			if($data['shopid']!=SHOPID) $this->goall($this->lang['die_access'],1,0,"/index.php");
			$t_d=M("shop_product_data")->selectRow(array("where"=>"id=$id"));
			if(!empty($t_d)){
				$data=array_merge($data,$t_d);
			}		
		}else{
			$data=M("shop_product")->selectRow(array("where"=>"is_temp=1 AND shopid=".SHOPID));
				if(empty($data)){
					$id=M("model_index")->insert(array("tablename"=>"shop_product"));
					M("shop_product")->insert(array("id"=>$id,"dateline"=>time(),"is_temp"=>1,"status"=>99,"shopid"=>SHOPID));
					M("shop_product_data")->insert(array("id"=>$id,"dateline"=>time()));
					M("shop_product_attr")->insert(array("attr_content"=>" ","dateline"=>time(),"id"=>$id));
					$data['id']=$id;
				}
		}
		$attr=M("shop_product_attr")->selectRow(array("where"=>"id=$id "));
		$this->smarty->assign(
			array(
				"cat_list"=>$cat_list,
				"data"=>$data,
				"shop_cat_list"=>M("shop_category")->children(array("where"=>"shopid=".SHOPID)),
				"attr_cat"=>M("attribute_cat")->attr_cat(),
			)
		);
		$this->smarty->display("shop_product/add.html");
	}
	
	public function onSave(){
		$id=post('id','i');
		$data=M("shop_product")->postData();
		if($id){
			$row=M("shop_product")->selectRow("id=".$id);
			if($row['is_temp']==1){
				$data['is_temp']=0;
				$data['status']=2;
			}
			if($row['shopid']!=SHOPID) $this->goall($this->lang['die_access'],1,0,"/index.php");
		}
		$data['title']=post('title','h');	
		$data['last_time']=time();		
		$data['keywords']=post('keywords','h');
		$data['description']=post('description','h');
		if(empty($data['description'])){			
			$data['description']=cutstr(strip_tags($_POST['content']),240);
		}
		$data['catid']=post('catid','i');
		$data['shop_cid']=post('shop_cid','i');
		$cat=M("category")->selectRow("catid=".$data['catid']);
		if(empty($cat)) $this->goall($this->lang['cat_empty'],1);
		$data['model_id']=$cat['model_id'];
		$data['imgurl']=post('imgurl','h');
		if($data['imgurl']){
			$data['is_img']=1;
		}else{
			$data['is_img']=0;
		}				
		if($data['imgurl']){			 
			$data['is_img']=1;			 
		}
		
		//gps信息
		if(post('latlng')){
			$latlng=explode(",",post('latlng'));
			$data['lat']=round($latlng[0],6);
			$data['lng']=round($latlng[1],6);
			if ($data['lat'] && $data['lng']){
					$data['isgps']=1;
			}
		}
		
		$sdata=array(
			"id"=>$id,
			"content"=>post('content','x'),
			"dateline"=>time()
		);
		/*扩展信息*/
		$data['shopid']=SHOPID;
		$data['price']=post('price','r','1');
		$data['lower_price']=post('lower_price','r',1);
		$data['total_num']=post('total_num','i');
		/*END 扩展信息*/
		/*属性处理*/
		$post_att=post('attr');
		$col_attr=M("attribute")->col_name_attr(post('attr_cat_id','i'));
		if($col_attr){
			foreach($col_attr as $k=>$v){
				$data[$v]=intval($post_att[$k]);
			}
		}
	 
		
		$data['attr_cat_id']=post('attr_cat_id','i');
		
		
		$attr_cat_id=post('attr_cat_id','i');
		
		$attr_content=base64_encode(json_encode(stripslashes_deep(post('attr','x'))));
		/*End 属性*/
		if($id){			
			M("shop_product")->update($data,array("id"=>$id));
			if(!M("shop_product_data")->selectRow("id=$id")){
				M("shop_product_data")->insert($sdata);
			}else{
				M("shop_product_data")->update($sdata,array("id"=>$id));
			}
			if(M("shop_product_attr")->selectRow(array("where"=>" id=$id "))){
					M("shop_product_attr")->update(array(
						"attr_content"=>$attr_content,
						"attr_cat_id"=>$attr_cat_id
					),"id=$id");
			}else{
					M("shop_product_attr")->insert(array(
						"id"=>$id,
						"attr_content"=>$attr_content,
						"attr_cat_id"=>$attr_cat_id,
						"dateline"=>time()
					));
			}
		}else{
			$data['userid']=M("login")->userid;
			$data['dateline']=time();
			
			$data['id']=$id=M("model_index")->insert(array("tablename"=>"shop_product"));
			$sdata['id']=$id;
			if(M("shop_product")->insert($data)){
				M("shop_product_data")->insert($sdata);
			}
			M("shop_product_attr")->insert(array(
						"id"=>$id,
						"attr_content"=>$attr_content,
						"attr_cat_id"=>$attr_cat_id,
						"dateline"=>time()
			));
		}
	 
		$this->goall($this->lang['save_success'],0,$data,APPSHOP."?m=shop_product&a=add&id=".$id);
	}
	
	public function onDelete(){
		$id=get('id','i');
		$row=M("shop_product")->selectRow("id=".$id);
		if($row['shopid']!=SHOPID) $this->goall($this->lang['die_access'],1,0,"/index.php");
		M("shop_product")->update(array("status"=>99),"id=".$id);
		$this->goall($this->lang['delete_success']);
	}
	
	public function onattrByCat(){
		 $id=get('id','i');
		 $cat=M("category")->selectRow(array("where"=>" catid=$id "));
		 if($cat['attr_cat_id']){
			
		 	$cat_id=$cat['attr_cat_id'];
			$data=M("attribute")->getAttr($cat_id);
			$this->smarty->assign(array(
				"data"=>$data,
				"attr"=>$attr,
				"attr_cat_id"=>$cat_id
			));
			$this->smarty->display("attr/attr.html"); 
		 }
	 }
	 
	 
	 public function onAttr(){
		
		 $cat_id=get('cat_id','i');
		 $id=get('id','i');
		 $attr=array();
		 if($id){
			$row=M("shop_product_attr")->selectRow(array("where"=>" id=$id AND attr_cat_id=".$cat_id." "));
			
			if($row){ 
				$attr=json_decode(base64_decode($row['attr_content']),true);
				 
			}
		 }
		$data=M("attribute")->getAttr($cat_id);
		$this->smarty->assign(array(
			"data"=>$data,
			"attr"=>$attr,
			"attr_cat_id"=>$cat_id
		));
		$this->smarty->display("attr/attr.html");
		
	 }
	 
	 public function onCategory(){
		$ids=post('ids','i');
		$catid=post('catid','i');
		if(!$catid) $this->goall("请选择分类",1);
		if($ids){
			foreach($ids as $id){
				M("shop_product")->update(array("catid"=>$catid),"id=".$id);
			}
		}
		$this->goall("修改成功");
	}
	
	public function onExportExcel(){
		require_once ROOT_PATH. 'PHPExcel/Classes/PHPExcel.php';
		$objPHPExcel = new PHPExcel();
       		$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(36); 
            $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(18); 
            $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(16);
            $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(16);
            $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(18);    
            $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(18); 
            $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(36); 
     
			$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'ID')
            ->setCellValue('B1','名称')           
            ->setCellValue('C1', '分类')
            ->setCellValue('D1','价格')            
            ->setCellValue('E1', '库存') 
            ->setCellValue('F1', '图片')
            ->setCellValue('G1', '销量')            
            ->setCellValue('H1', '市场价')           
            ->setCellValue('I1', '内容')
              
            ;
	}
	
	public function onImportExcel(){
		require_once ROOT_PATH. 'PHPExcel/Classes/PHPExcel.php';
		
	}
	//导出json
	public function onExJson(){
		
	}
	//导入json
	public function onImJson(){
		
	}
}
?>